// pages/goodsDetail/goodsDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalCount: 0,
    totalMoney: 0,
    bigImage: "logo.jpg",
    selectedIndex: 0,
    selectedProduct: 0,
    selectImage: "logo.jpg",
    name:0,
    time:0,
    detail:0,
    price:0,
    /*选择的索引，默认选第0个*/
    disabled: true,
    checkxx: true,
    flag:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let mid = options.id;
    let that = this;

    wx.request({
      url: 'http://localhost:8080/wxservice/servlet/GoodsDetailServlet?',
      data: {
        id: mid
      },
      success: function(res) {
        console.log(res.data);
        wx.setNavigationBarTitle({
          title: res.data.title,
        });
        that.setData({
          images: res.data.images,
          goods: res.data.goods,
          bigImage: res.data.images[0],
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  selectImage: function(e) {
    let index = e.currentTarget.dataset.id;
    this.setData({
      selectedIndex: index,
      bigImage: this.data.images[index],
    });
  },
  plusGoods: function(e) {
    let index = e.currentTarget.dataset.index;
    let myGoods = this.data.goods;
    myGoods[index].count++;
    this.setData({
      goods: myGoods,
      totalCount: this.data.totalCount + 1,
      totalMoney: this.data.totalMoney + myGoods[index].price,
      disabled: false
    });
  },
  minusGoods: function(e) {
    let index = e.currentTarget.dataset.index;
    let myGoods = this.data.goods;
    myGoods[index].count--;
    
    this.setData({
      goods: myGoods,
      totalCount: this.data.totalCount - 1,
      totalMoney: this.data.totalMoney - myGoods[index].price,

      });   

  },

  inputGoods: function (e) {
    let index = e.currentTarget.dataset.index;
    let myGoods = this.data.goods;
    let oldCount = myGoods[index].count;
    let newCount = e.detail.value;

    let minusCount = newCount - oldCount;
    let minusMoney = minusCount * myGoods[index].price;

    myGoods[index].count = newCount;

    this.setData({
      goods: myGoods,
      totalCount: this.data.totalCount + minusCount,
      totalMoney: this.data.totalMoney + minusMoney,
    });
  } ,
  buySomething(e) {
    wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000
    })
  },

  checkDrink(e) {
    this.setData({
      checkxx: false,
      flag:false,
    })
  },
  close(e) {

    this.setData({
      checkxx: true,
      flag:true,
    });
  },

  selectImage1: function(e) {
    let index = e.currentTarget.dataset.index;
    let that=this;
    wx.request({
      url: 'http://localhost:8080/wxservice/servlet/ItemGoods',
      data:{index:index},
      success: function (res) {
        that.setData({
          selectImage: res.data.imgPath,
          name: res.data.name,
          time: res.data.time,
          detail: res.data.item,
          price: res.data.price,
        });
      }
    });
  },

})